package cn.itcast.web.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class imageServlet
 */
@WebServlet("/imageServlet")
public class imageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final int WIDTH = 120;
	private static final int HEIGHT = 25;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB );
		Graphics g = image.getGraphics();
		setBackgroud(g);
		setBorder(g);
		setRandomLine(g);
		//setRandomData(g);
		String randomChar = setRandomDataChinese(g);
		
		response.setHeader("Expires", "-1");
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Pragma", "no-cache");
		
		response.setContentType("image/jpeg");
		ImageIO.write(image, "jpg", response.getOutputStream());
		request.getSession().setAttribute("randomChar", randomChar);
		
		
	}
	
	private String setRandomDataChinese(Graphics g) {
		g.setColor(Color.RED);
		g.setFont(new Font("宋体",Font.BOLD,16));
		String base = "\u7684\u4e00\u662f\u4e86\u6211\u5782\u4ed6\u5927\u5730\u5230\u548c\u751f\u591a\u5fc3\u4ee5\u5b66\u4e48\u770b\u9053\u73b0\u4f46\u524d\u6b21\u8f66\u65b0\u5c06\u5e76\u6bd4";
		StringBuffer s = new StringBuffer();
		for(int i=0;i<5;i++) {
			int num = new Random().nextInt(base.length());
			s.append(base.charAt(num)); //charAt(index) 返回指定索引处的char值 
		}
		g.drawString(s.toString(), 10, 20);
		return s.toString();
	}
	
	private void setRandomData(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(Color.RED);
		Font font = new Font("宋体",Font.BOLD,20);
		g.setFont(font);
		StringBuffer s = new StringBuffer();
		for(int i=0;i<5;i++) {
			int number = new Random().nextInt(10);
			s.append(number + " ");
		}
		String data = s.toString();
		g.drawString(data, 10, 20);
	}

	private void setRandomLine(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(Color.GREEN);
		for(int i=0;i<10;i++) {
			int x1 = new Random().nextInt(WIDTH);
			int y1 = new Random().nextInt(HEIGHT);
			int x2 = new Random().nextInt(WIDTH);
			int y2 = new Random().nextInt(HEIGHT);
			//Graphics2D g2 = (Graphics2D)g;
			//g2.setStroke(new BasicStroke(3.0f));
			g.drawLine(x1, y1, x2, y2);
		}
	}

	private void setBackgroud(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(Color.WHITE);
		g.fillRect(0, 0, WIDTH, HEIGHT);
		
	}

	private void setBorder(Graphics g) {
		// TODO Auto-generated method stub
		g.setColor(Color.RED);
		g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
		
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
